<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of surat_model
 *
 * @author brlnt
 */
class User_model extends CI_Model {

    //put your code here
    function User_model() {
        parent::__construct();
        $this->CI = get_instance();
    }
	
	function getSingleUser($id){
		$this->db->select('*');
		$this->db->from('loginuser');
		$this->db->where('idloginuser', $id);
		return $this->db->get();
	}
	
	function gantiNama($id, $data){
		$this->db->where('idloginuser', $id);
		$this->db->update('loginuser', $data); 
	}
	
	function addUser($data){
		$this->db->insert('loginuser', $data);
	}
	
	function gantiPassword($id, $data){
		$this->db->select('password_user');
		$this->db->from('loginuser');
		$this->db->where('idloginuser', $id);
		$query = $this->db->get();
        //if ($query->num_rows() > 0){
		$pwd_lama = null;
		foreach($query->result() as $row){
			$pwd_lama = $row->password_user;
		}
		
		if($pwd_lama == md5($data['pwd_lama'])){
			$upData['password_user'] = md5($data['pwd_baru']);
			$this->db->where('idloginuser', $id);
			$this->db->update('loginuser', $upData);
			return true;
		} else{
			return false;
		}
	}
	
	function getAllUser($start=0, $limit=0) {		
		$query = "SELECT idloginuser, nama_user, nama_lengkap, role ";
		$query .= "FROM loginuser ";
		if($start+$limit != 0){
			$query .= "LIMIT ".$start.",".$limit;
		}
		
		$hasil = $this->db->query($query);
		return $hasil;
    }

    function delete_user($id){
    	$this->db->where('idloginuser', $id);
    	$this->db->delete('loginuser');
    	return $this->db->affected_rows();
    }

    function ubah_role($id, $role){
    	$data['role'] = $role;
    	$this->db->where('idloginuser', $id);
    	$this->db->update('loginuser', $data);
    	return $this->db->affected_rows();
    }
}